﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;

    public class SkuPickLoc : BasePage
    {
        protected Button btnGoBack;
        protected Button btnSave;
        protected CheckBox chkAllowReplenishFromBulk;
        protected CheckBox chkAllowReplenishFromCasepick;
        protected CheckBox chkAllowReplenishFromPiecepick;
        protected DropDownList ddlLocationType;
        protected DropDownList ddlReplenishmentUom;
        protected HtmlForm F2065;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLocID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnSkuID;
        protected HtmlHead Head1;
        protected Label lblAreaDetail;
        protected Label lblLocationType;
        protected Label lblLocID;
        protected Label lblMsg;
        protected Label lblQtyLocationLimit;
        protected Label lblQtyLocationMinimum;
        protected Label lblReplenishmentPriority;
        protected Label lblReplenishmentUom;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtLocID;
        protected TextBox txtQtyLocationLimit;
        protected TextBox txtQtyLocationMinimum;
        protected TextBox txtReplenishmentPriority;

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("Sku.aspx?from=" + this.hdnFrom.Value + "&ID=" + this.hdnOwnerID.Value + "&ID2=" + this.hdnSkuID.Value + "&currTab=5", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLocID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2110_017");
            }
            else
            {
                InvSkuLocInfo invSkuInfo = null;
                if (!string.IsNullOrEmpty(this.hdnLocID.Value))
                {
                    invSkuInfo = base.DataEntityViewState as InvSkuLocInfo;
                }
                else
                {
                    invSkuInfo = new InvSkuLocInfo();
                }
                invSkuInfo.Loc = this.txtLocID.Text;
                if (!string.IsNullOrEmpty(this.txtQtyLocationLimit.Text))
                {
                    invSkuInfo.QtyLocationLimit = decimal.Parse(this.txtQtyLocationLimit.Text);
                }
                else
                {
                    invSkuInfo.QtyLocationLimit = 0M;
                }
                if (!string.IsNullOrEmpty(this.txtQtyLocationMinimum.Text))
                {
                    invSkuInfo.QtyLocationMinimum = decimal.Parse(this.txtQtyLocationMinimum.Text);
                }
                else
                {
                    invSkuInfo.QtyLocationMinimum = 0M;
                }
                invSkuInfo.OwnerID = this.hdnOwnerID.Value;
                invSkuInfo.SkuID = this.hdnSkuID.Value;
                invSkuInfo.ReplenishmentPriority = this.txtReplenishmentPriority.Text;
                if (!string.IsNullOrEmpty(this.ddlReplenishmentUom.SelectedValue))
                {
                    invSkuInfo.ReplenishmentUom = this.ddlReplenishmentUom.SelectedValue;
                }
                invSkuInfo.AllowReplenishFromBulk = this.chkAllowReplenishFromBulk.Checked ? 1 : 0;
                invSkuInfo.AllowReplenishFromCasepick = this.chkAllowReplenishFromCasepick.Checked ? 1 : 0;
                invSkuInfo.AllowReplenishFromPiecepick = this.chkAllowReplenishFromPiecepick.Checked ? 1 : 0;
                invSkuInfo.LocationType = this.ddlLocationType.SelectedValue;
                try
                {
                    if (!string.IsNullOrEmpty(this.hdnLocID.Value))
                    {
                        invSkuInfo.UpdatedBy = base.UserInfo.UserID;
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2110_018") + BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(invSkuInfo, "INV_SKU_LOC") + base.GetGlobalMsgResource("C002");
                    }
                    else
                    {
                        invSkuInfo.CreatedBy = base.UserInfo.UserID;
                        this.lblMsg.Text = base.GetGlobalMsgResource("F2110_018") + BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(invSkuInfo, "INV_SKU_LOC") + base.GetGlobalMsgResource("C005");
                    }
                    base.Response.Redirect("Sku.aspx?from=" + base.UrlEncode(this.hdnFrom.Value) + "&ID=" + base.UrlEncode(this.hdnOwnerID.Value) + "&ID2=" + base.UrlEncode(this.hdnSkuID.Value) + "&currTab=5", false);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F2110_019") + ex.Message;
                }
            }
        }

        private void InitDropDrowList()
        {
            base.BindData2DDLBySysCode(this.ddlLocationType, "LOCUSAGE", true);
            base.BindData2DDLBySysCode(this.ddlReplenishmentUom, "TUOM", true);
        }

        private void LoadData()
        {
            this.InitDropDrowList();
            this.hdnOwnerID.Value = base.Request.QueryString["ID"];
            this.hdnSkuID.Value = base.Request.QueryString["ID2"];
            this.hdnFrom.Value = base.Request.QueryString["from"];
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID3"]))
            {
                this.hdnLocID.Value = base.Request.QueryString["ID3"];
                InvSkuLocInfo InvSkuLoc = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnOwnerID.Value, this.hdnSkuID.Value, this.hdnLocID.Value, "INV_SKU_LOC") as InvSkuLocInfo;
                base.DataEntityViewState = InvSkuLoc;
                if (InvSkuLoc != null)
                {
                    this.ddlLocationType.SelectedValue = InvSkuLoc.LocationType;
                    this.txtLocID.Text = InvSkuLoc.Loc;
                    this.txtQtyLocationLimit.Text = InvSkuLoc.QtyLocationLimit.ToString();
                    this.txtQtyLocationMinimum.Text = InvSkuLoc.QtyLocationMinimum.ToString();
                    this.txtReplenishmentPriority.Text = InvSkuLoc.ReplenishmentPriority;
                    this.ddlReplenishmentUom.Text = InvSkuLoc.ReplenishmentUom.ToString();
                    this.chkAllowReplenishFromBulk.Checked = InvSkuLoc.AllowReplenishFromBulk == 1;
                    this.chkAllowReplenishFromCasepick.Checked = InvSkuLoc.AllowReplenishFromCasepick == 1;
                    this.chkAllowReplenishFromPiecepick.Checked = InvSkuLoc.AllowReplenishFromPiecepick == 1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }
    }
}

